//权限,路由
import { RouteLocationNormalized } from 'vue-router'
import router, { asyncRouterList, page404 } from '@/router';
import { useStore } from './store/user'
import { getCookie } from './utils/auth'
import NProgress from 'nprogress'; // progress bar
import 'nprogress/nprogress.css'; // progress bar style
import { useTitle } from '@vueuse/core';

NProgress.configure({ showSpinner: false });


//白名单
const whiteListRouters = [];
let startTime = Date.now()
let currentTime: number;
// 全局路由守卫
router.beforeEach((to: RouteLocationNormalized, from: RouteLocationNormalized, next) => {
    console.log("🚀 ~ file: index.ts ~ line 51 ~ router.beforeEach ~ to", to)
    //记录当前页面访问时长
    if (to) {
        currentTime = Date.now()
        console.log(`用户由 ${from.path} 页面 跳转到 ${to.path} 页面，在 ${from.fullPath} 页面停留了 ${(currentTime - startTime) / 1000} s`)
        startTime = Date.now()
    }

    //设置title
    const currEntTitle: string = to.meta?.title ?? to.name
    const title = useTitle()
    title.value = currEntTitle

    //判断是否登陆
    const login = getCookie()
    console.log("🚀 开始判断是否登陆")

    // if (login) {
    //     if (to.name == 'Login') {
    //         next('/')
    //     } else {
    //         const user = useStore()
    //         user.GetInfo().then((response) => {
    //             console.log("🚀获得返回的用户信息", response)
    //             next()
    //         }).catch((error) => {
    //             console.log("🚀获得用户信息出错了", error)
    //             Message.error(error)
    //             next('/login')
    //         })
    //         next()
    //     }
    // } else {
    //     if (to.name == 'Login') {
    //         next()
    //     } else {
    //         next('/login')
    //     }
    // }
    next()
})

router.afterEach(() => {
    NProgress.done();
});
